// Fill out your copyright notice in the Description page of Project Settings.

#include "MyWidgetBlueprintLibrary.h"


void UMyWidgetBlueprintLibrary::MyRotateActorAround(class AActor* actor, FVector pos, FVector axis, float angle)
{
	if (actor == NULL)
		return;

	auto mypos = actor->GetActorLocation();
	FQuat q(axis, FMath::DegreesToRadians(angle));
	auto va = mypos - pos;
	auto vb = q.RotateVector(va);

	//axis = actor->GetActorTransform().Rotator().GetInverse().RotateVector(axis);
	
	FVector oldv = actor->GetActorRotation().Vector();
	FVector v = actor->GetActorRotation().Vector().RotateAngleAxis(angle, axis);

	actor->SetActorLocation(pos + vb, false, NULL, ETeleportType::TeleportPhysics);
	actor->SetActorRotation(v.Rotation().Quaternion(), ETeleportType::TeleportPhysics);

	UE_LOG(LogBlueprintUserMessages, Log, TEXT("actor=%s, old=%s, %s, %f\n"), *actor->GetFullName(), *oldv.ToString(), *v.ToString(), angle);
}
FTransform UMyWidgetBlueprintLibrary::MyRotateAround(FTransform transform, FVector pos, FVector axis, float angle)
{
	FTransform ret = FTransform::Identity;

	auto mypos = transform.GetLocation();
	FQuat q(axis, FMath::DegreesToRadians(angle));
	auto va = mypos - pos;
	auto vb = q.RotateVector(va);
	ret.SetLocation(pos + vb);

	FVector oldaxis = axis;
	axis = transform.GetRotation().Inverse().RotateVector(axis);
	auto localaxis = axis;
	q = FQuat(localaxis, FMath::DegreesToRadians(angle));
	q = transform.GetRotation() * q;
	ret.SetRotation(q);

	UE_LOG(LogBlueprintUserMessages, Log, TEXT("%s, %s, %f\n"), *q.ToString(), *axis.ToString(), angle);
	UE_LOG(LogBlueprintUserMessages, Log, TEXT("old=%s, new=%s\n"), *oldaxis.ToString(), *axis.ToString());

	return ret;
}
FTransform UMyWidgetBlueprintLibrary::GhRotateAround(FTransform transform, FVector pos, FVector axis, float angle)
{	
	return MyRotateAround(transform, pos, axis, angle);
}